package com.team.game.Screens;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.view.MotionEvent;

import com.team.game.Assets;
import com.team.game.Game.Gestures;
import com.team.game.Game.Levels;

import com.team.game.Game.Map_Background;
import com.team.game.Game.Map_Foregroung;
import com.team.game.Game.Pause;


import com.team.game.Game.Towers.TowerHandler;
import com.team.game.framework.Level;
import com.team.game.framework.Screen;
import com.team.game.framework.Game;
import com.team.game.framework.impl.SurfaceThread;
import com.team.game.framework.impl.MainActivity;
import com.team.game.framework.impl.Scale_X_Y;

import java.io.IOException;
import java.io.InputStream;

import static com.team.game.framework.impl.Scale_X_Y.Scale_X;
import static com.team.game.framework.impl.Scale_X_Y.Size_X;
import static com.team.game.framework.impl.Scale_X_Y.Size_Y;

public class Game_Screen extends Screen {

    private final TowerHandler towers;
    private final Level levels;
    private final Pause pause;
    private Map_Background map_background;
    private Map_Foregroung map_foregroung;
    private Gestures gestures;


    private final RectF rectPause = new RectF ();

    private Bitmap pauseButton;
    private boolean isPause = false;

    public Game_Screen (Game game, int level) {
        super(game);

        SurfaceThread.wait = true;
        int[] wizz = {1};
        //устанавливаем обработку жестов для первой локации
        game.setGestures(wizz, 1);




        InputStream inputStream;
        try {
            inputStream = MainActivity.manager.open("Picture/test_pause.png");
            pauseButton = Bitmap.createScaledBitmap (BitmapFactory.decodeStream (inputStream), (int) (98 * Scale_X), (int) (98 * Scale_X), false);

            inputStream = MainActivity.manager.open ("Picture/test_pause_menu.png");
            Assets.pause = Bitmap.createScaledBitmap (BitmapFactory.decodeStream (inputStream), (int) Size_X, (int) Size_Y, false);


        } catch (IOException e) {
            e.printStackTrace();
        }

        map_background = new Map_Background(level);
        map_foregroung = new Map_Foregroung(level);

        towers = new TowerHandler (game, level);
        levels = new Levels (game, level);
        gestures = new Gestures(game, 1, wizz);
        pause = new Pause ();

//98, 98

        //для круга используем одинаковые коеф скейлинга, а то получится яйцо
        rectPause.set (937 * Scale_X_Y.Scale_X, 45 * Scale_X_Y.Scale_X, 1035 * Scale_X_Y.Scale_X, 143 * Scale_X_Y.Scale_X);

        SurfaceThread.wait = false;
    }

    @Override
    public void update() {
        if (!isPause) {
            levels.update ();
            towers.update ();
            gestures.update();
        }
    }

    @Override
    public void present(Canvas canvas) {

        map_background.draw(canvas);
        //mobs
        levels.drawMobs (canvas);


        map_foregroung.draw(canvas);
        //gestures
        //towers
        towers.present(canvas);
        gestures.draw (canvas);

        //pause menu
        if (isPause)
            pause.draw (canvas);

        //pause/unpause button
        canvas.drawBitmap (pauseButton, null, rectPause, null);
    }

    @Override
    public void pause() {
        isPause = true;
        gestures.pause (true);
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
        pauseButton.recycle ();
        map_background.dispose();
        levels.dispose ();
    }

    private byte firstBack = 0;

    @Override
    public void back() {
        /* При нажатии на кнопку назад во время игры - вызывается пауза
         * При повторном нажатии переход на карту */
        if (firstBack == 0 && !isPause) { // Если переменная не инкрементирована
            gestures.pause (true);
            isPause = true;
            firstBack++;
        } else
            game.setScreen (new Maps_Screen (game));
    }

    private boolean GesturesTapDown = false;
    @Override
    public boolean touchEvent (MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            if (!isPause) {
                //При нажатии кнопки паузы/возвращения в игру
                if (Assets.inRect(rectPause, motionEvent)) {
                    isPause = !isPause;
                    gestures.pause(isPause);
                    GesturesTapDown = false;
                } else {
                    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                        if (!towers.Tap(motionEvent)) {
                            gestures.TapDown(motionEvent);
                            GesturesTapDown = true;
                        } else GesturesTapDown = false;
                    }
//                else if (motionEvent.getAction() == MotionEvent.ACTION_UP && !SurfaceThread.wait) {
//                    towers.Tap(motionEvent);
//                }
                }
            } else {
                //При нажатии кнопки паузы/возвращения в игру
                if (Assets.inRect(rectPause, motionEvent)) {
                    isPause = !isPause;
                    gestures.pause(isPause);
                } else {
                    switch (pause.touch(motionEvent)) {
                        case 1: {
                            isPause = false;
                            gestures.pause(isPause);
                            break;
                        }
                        case 2: {
                            towers.towersRestart();
                            levels.restart();
                            isPause = false;
                            gestures.pause(isPause);
                            break;
                        }
                        case 3: {
                            game.setScreen(new Maps_Screen(game));
                            break;
                        }
                    }
                }
            }
        }
        if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
            if (GesturesTapDown)
                gestures.TapDown(motionEvent);
        }
        return true;
    }

    @Override
    public Level getLevel() {
        return this.levels;
    }

    @Override
    public Gestures getGestures() {
        return gestures;
    }
}